#include<stdio.h>
#include<at89c51ed2.h>
#include<mcs51reg.h>
#include<malloc.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

#include "glcd.h"
#include "io.h"
#include "eeprom.h"



extern void i2c_init(int block_select);
extern void i2c();
extern void i2c_read();
extern int i2c_write();
extern int i2c_sendData(int val);
extern int i2c_readData();
extern void i2c_stop();
extern int i2c_ack();
extern void i2c_noack();
extern void i2c_expandinit();
extern void i2c_reset();
extern void i2c_sendDataPage(int val);
extern void i2c_rtcinit();


/*note: follow sequence to set time
 * 
rtcsetYear(12);
rtcSetDDMM(2,12);
rtcSetHour(23);
rtcsetMin(02);
this is done since stack cannot take more that 2 variables*/

int rtcRead(int addr)
{
	int ack;	
	int readV;
	
	i2c_rtcinit();
	 
	i2c_write();
	
	ack=i2c_sendData(addr);
	
	i2c_rtcinit();
	
	i2c_read();
	
	readV=i2c_readData();
	
	ack=i2c_ack();
	
	i2c_stop();
	
	return readV;
	
}


void rtcWrite(int addr,int val)
{
	i2c_rtcinit();
	
	i2c_write();
	
	i2c_sendData(addr);
	
	i2c_sendData(val);
	
	i2c_stop();
}



void rtcStart()
{
	rtcWrite(0x00,0x00); //clear CH bit to run clock
}

void rtcStop()
{
	rtcWrite(0x00,0x80); //set CH bit to stop clock
}


int int2BCD(int integer) //convert decimal value to BCD 
{
int mult;
char val[5];
int i;
int sum;


mult=1;
sum=0;

_itoa(integer,val,10);  //convert to ASCII string


for(i=0;i<strlen(val);i++)
{
sum=sum+mult*(val[strlen(val)-i-1]-48); //take each string value ASCII, subtract 48 to convert to integer
mult=mult*16;
}

return sum;
}



void rtcSetDDMM(int date,int Month) //sets date and month
{
	int dateBCD;
	int MonthBCD;
	
	
	dateBCD=int2BCD(date);
	MonthBCD=int2BCD(Month);


	rtcStop();
	rtcWrite(0x04,dateBCD); //write date
	rtcWrite(0x05,MonthBCD); //write month
	rtcStart();
}

void rtcsetYear(int year) //sets year
{
	
	int yearBCD;

	rtcStop();
	yearBCD=int2BCD(year);
	rtcWrite(0x06,yearBCD); //write year
	rtcStart();
}
	
void rtcSetHour(int hours) //sets hours
{
	int hoursBCD;

	
	/*not implementing day*/
	
	hoursBCD=int2BCD(hours); //convert to BCD format required by RTC

	rtcStop();
	rtcWrite(0x02,hoursBCD); //write hours
	rtcStart();
	
	
}

void rtcsetMin(int min)
{
	int minBCD;
	
	minBCD=int2BCD(min);

	rtcStop();
	rtcWrite(0x01,minBCD); //write minutes
	rtcStart();
}

int readMin()
{
	
	char temp[5];

	_itoa(rtcRead(0x01),temp,16); //convert read BCD value to string of integer 
	return atoi(temp);//return converted to integer value
}

int readHour()
{
	char temp[5];
	
	_itoa(rtcRead(0x02),temp,16);
	return atoi(temp);
}

int readDate()
{
	char temp[5];
	
	_itoa(rtcRead(0x04),temp,16);
	return atoi(temp);	
}

int readMonth()
{
	char temp[5];
	
	_itoa(rtcRead(0x05),temp,16);
	
	return atoi(temp);
}

int readYear()
{
	char temp[5];
	
	_itoa(rtcRead(0x06),temp,16);
	return atoi(temp);	
}



/*takes 18 pixels*/
void printTime(int col,int select)
{
unsigned char val[5];	
unsigned char min;
unsigned char hour;
unsigned char year;
unsigned char page;

page=7;


if(select==1)
{
/*read hour and min*/
hour=readHour();
min=readMin();


if(hour<10)
{
	 drawString("0",page,col);
	_itoa(hour,val,10);
	 drawString(val,page,col+4);
	 drawString(":",page,col+8);
	 
		if(min<10){
		drawString("0",page,col+12);
		_itoa(min,val,10);
		drawString(val,page,col+16);
		}
		else
		{
		_itoa(min,val,10);
		drawString(val,page,col+12);
		}
}
else
{
	_itoa(hour,val,10);
	 drawString(val,page,col);
	 drawString(":",page,col+8);
	
		if(min<10){
		drawString("0",page,col+12);
		_itoa(min,val,10);
		drawString(val,page,col+16);
		}
		else
		{
		_itoa(min,val,10);
		drawString(val,page,col+12);
		}
}

}
if(select==2)
{
/*read date*/
hour=readDate(); //hour=date
min=readMonth();//min=month
year=readYear();


	drawString("12",page,col);  //see this problem later, month is printed twice
		
		
			if(hour<10)
			{
			drawString("|0",page,col+8);
			_itoa(hour,val,10);
			drawString(val,page,col+16);
			}
			else
			{
			drawString("|",page,col+8);
			_itoa(hour,val,10);
			drawString(val,page,col+12);
			}
		
		drawString("|",page,col+20);
		drawString("12",page,col+24);  //see this problem later, month is printed twice
		//drawString(year,0,col+23);
		
		}
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	


